<!DOCTYPE html>

<html>

<head>
	<meta charset="utf-8" />
	<link rel="stylesheet" type="text/css" href="style.css" />
	<title>Inscription</title>

</head>


<body>

	<div class="top-menu">
		<ul>
			<li ><a class="top_left-menu" href="#" >Link 1</a></li>
			<li><a href="#">Link 2</a></li>
			<li><a href="#">Link 3</a></li>
			<li><a href="#">Link 4</a></li>
			<li><a class="bot_left-menu" href="#">Link 5</a></li>
		</ul>
		<br />
	</div>


	<div class="inscription-box">

	<?php
			if(!isset($_GET['page']))
				{
		?>
					<form action="inscription.php?page=1" method="post">
						<input type="text" name="login" placeholder="Login" /><br />
						<input type="text" name="mdp" placeholder="Password"/><br />
						<input type="text" name="email" placeholder="Mail address"/><br />
						<input type="text" name="nom" placeholder="Name"/><br/ >
						<input type="text" name="prenom" placeholder="Frist name"/><br />						
						<input type="submit" value="OK"><br />
					</form>
		<?php
				}
			else 
				{
					$error = false;
					if (strlen($_POST['login']) < 3 || strlen($_POST['login']) > 255 || empty($_POST['login']))
						{
							$errorToAff = '<p>Le login n\'est pas conforme<p>';
							$error = true;
						}
					if (strlen($_POST['mdp']) < 3 || strlen($_POST['mdp']) > 255 || empty($_POST['mdp']))
						{
							$errorToAff .= '<p>Le mot de passe n\'est pas conforme</p>';
							$error = true;
						}
					if (!preg_match("#^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]{2,}\.[a-z]{2,4}$#", $_POST['email']) || empty($_POST['email']))
						{
							$errorToAff .= '<p>L\'email n\'est pas conforme</p>';
							$error = true;
						}
					if (empty($_POST['nom']))
						{
							$errorToAff .= '<p>Veuillez entrer un nom</p>';
							$error = true;
						}
					if (empty($_POST['prenom']))
						{
							$errorToAff .= '<p>Veuillez entrer un pr&ecute;nom</p>';
							$error = true;
						}
					if ($error)
						{
							echo $errorToAff;
							echo '</ br><a href="inscription.php">Retour a l\'inscription</a>';
						}
					else 
						{
							$mysqli = new mysqli("localhost", "root", "root", "forum");

							/* Vérification de la connexion */
							if (mysqli_connect_errno())
								{
    								echo 'Échec de la connexion : ' . mysqli_error();
    								exit;
								}
							$requete = 'INSERT INTO user (login, mdp, email, nom, prenom, isAdmin, isModo) VALUES (\'' . $_POST['login'] . '\', \'' . sha1($_POST['mdp']) . '\', \'' . $_POST['email'] . '\', \'' . $_POST['nom'] . '\', \'' . $_POST['prenom'] . '\', \'0\', \'0\');';
							if ($result = $mysqli->query($requete))
								{			
									echo '</ br><a href="index.php">Vous &ecirc;tes inscrit, veuillez vous connecter maintenant.</a>';
								}
							else 
								{
									echo '<p>La requ&ecirc;te a echouer</p>';
								}
							$mysqli->close();
						}
				}
		?>
	</div>
	<footer>
			<p>
				This is some text. <br />
				This is some text. <br />
				This is some text. <br />
				This is some text. <br />
			</p>
	</footer>

</body>

</html>